package cn.tworice.admin.web;

import cn.tworice.admin.service.LogService;
import cn.tworice.admin.service.LoginService;
import cn.tworice.admin.service.SystemService;
import cn.tworice.common.entity.Admin;
import cn.tworice.common.entity.Role;
import cn.tworice.common.entity.Resources;
import cn.tworice.common.entity.RoleResources;
import cn.tworice.common.entity.dto.ResourcesDto;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 系统管理模块 接口
 *
 * @author 二饭
 * @email 1786705138@qq.com
 */
@RestController
@RequestMapping("admin/system")
@Slf4j
@CrossOrigin
public class SystemController {

    @Autowired
    private SystemService systemService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private LogService logService;

    @ApiOperation("编辑或添加资源")
    @PutMapping("resources/edit")
    @ApiImplicitParam(name = "id", value = "如修改，请添加ID", dataType = "int", dataTypeClass = Integer.class)
    public RequestResult editResources(Resources resources, HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"添加/更新资源信息-"+resources.getName());

        RequestResult result = new RequestResult();
        int i = systemService.editResource(resources);
        if (i == 1) {
            result.setCode(new StatusCode(200, "操作成功"));
        } else {
            result.setCode(new StatusCode(500, "操作失败"));
        }
        return result;
    }

    @DeleteMapping("resources/del")
    @ApiOperation("删除资源")
    @ApiImplicitParam(name = "ids", value = "资源ID", required = true, dataType = "Array[Integer]", dataTypeClass = Integer[].class)
    public RequestResult delResources(Integer[] ids,HttpServletRequest request) {


        RequestResult result = new RequestResult();
        int i = systemService.delResources(ids);
        result.setCode(new StatusCode(200, "删除" + i + "条"));
        logService.addAdminLog(request.getHeader("adminID"),"删除" + i + "条资源");
        return result;
    }

    @GetMapping("resources/conditionQuery")
    @ApiOperation("通过条件查询资源内容")
    public RequestResult conditionQueryResources(String name) {
        RequestResult result = new RequestResult();
        List<ResourcesDto> list = systemService.selectResources(name);
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("list", list);
        return result;
    }

    @GetMapping("resources/list")
    @ApiOperation("获取所有资源列表")
    public RequestResult resourcesList() {
        RequestResult result = new RequestResult();
        List<ResourcesDto> list = loginService.getResources();
//         systemService.getResourceAll();
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("resourceList", list);
        return result;
    }

    @GetMapping("role/list")
    @ApiOperation("获取角色列表")
    public RequestResult roleList() {
        RequestResult result = new RequestResult();
        List<Role> list = systemService.getRoleList();
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("roleList", list);
        return result;
    }

    @GetMapping("role/adminList")
    @ApiOperation("通过角色ID获取人员列表")
    public RequestResult adminList(Integer roleId) {
        RequestResult result = new RequestResult();
        List<Admin> list = systemService.getAdminListByRoleId(roleId);
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("adminList", list);
        return result;
    }

    @PutMapping("role/adminToRole")
    @ApiOperation("添加人员到指定角色，可多人员")
    public RequestResult addAdminToRole(String[] adminId, Integer roleId,HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"添加人员-"+Arrays.toString(adminId)+"-到角色-"+roleId);
        int i = systemService.addAdminToRole(adminId, roleId);

        if (i == 1) {
            return new RequestResult(new StatusCode(200, "添加成功"));
        } else {
            return new RequestResult(new StatusCode(304, "请勿重复添加人员到角色"));
        }
    }

    @DeleteMapping("role/rmAdminToRole")
    @ApiOperation("从角色移除人员")
    public RequestResult rmAdminToRole(String[] adminIds, Integer roleId,HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"从角色-"+roleId+"-中移除人员"+Arrays.toString(adminIds));

        systemService.delAdminToRole(adminIds, roleId);
        return new RequestResult(new StatusCode(200, "操作成功"));
    }

    @GetMapping("role/getNoRoleAdmin")
    @ApiOperation("获取不是该角色的人员")
    public RequestResult getNoRoleAdmin(Integer roleId) {
        RequestResult result = new RequestResult();
        List<Admin> list = systemService.getNoRoleAdmin(roleId);
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("result", list);
        return result;
    }

    @GetMapping("role/getAdminByStatus")
    @ApiOperation("根据账号状态获取人员列表，0-正常，1-非正常")
    public RequestResult getAdminAll(Integer status) {
        RequestResult result = new RequestResult();
        List<Admin> list = systemService.getAdminByStatus(status);
        result.setCode(new StatusCode(200, "获取成功"));
        result.appendData("result", list);
        return result;
    }

    @GetMapping("role/getResourcesByRoleId")
    @ApiOperation("根据角色ID查询资源 关联数据")
    public RequestResult getResourcesByRoleId(Integer roleId) {
        RequestResult result = new RequestResult();
        List<RoleResources> list = systemService.getResourcesByRoleId(roleId);
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("result", list);
        return result;
    }

    /**
     * 获取角色权限列表 暂无更新
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/8 20:13
     */
    @GetMapping("role/resourcesByRole")
    @ApiOperation("根据角色ID查询资源 关联数据")
    public RequestResult resourcesByRole(Integer roleId) {
        RequestResult result = new RequestResult();
        List<RoleResources> list = systemService.getResourcesByRoleId(roleId);
        result.setCode(new StatusCode(200, "查询成功"));
        result.appendData("result", list);
        return result;
    }

    /**
     * 
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/6 22:29
     */
    @PutMapping("role/updateRoleResources")
    @ApiOperation("更新角色权限")
    public RequestResult updateRoleResources(@ApiParam("原有资源ID数组") Integer[] haveResources, @ApiParam("更新后的资源ID数组") Integer[] updateResources, Integer roleId,HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"更新角色-"+roleId+"-权限为"+Arrays.toString(updateResources));


        RequestResult result = new RequestResult();
        Boolean b = systemService.updateRoleResources(haveResources, updateResources, roleId);
        if (b) {
            result.setCode(new StatusCode(200, "更新成功"));
        }
        return result;
    }

    /**
     * 更新角色权限 叶子节点版
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/8 20:02
     */
    @PutMapping("role/updateResources")
    @ApiOperation("更新角色权限")
    public RequestResult updateResources(HttpServletRequest request,@ApiParam("角色ID") Integer roleId,@ApiParam("更新后的资源ID数组") Integer[] resourcesId){
        logService.addAdminLog(request.getHeader("adminID"),"更新角色-"+roleId+"-权限为"+Arrays.toString(resourcesId));

        RequestResult result = new RequestResult();
        Boolean b=systemService.setRoleResources(roleId,resourcesId);
        if (b) {
            result.setCode(new StatusCode(200, "更新成功"));
        }
        return result;
    }

    @DeleteMapping("role/delRole")
    @ApiOperation("删除角色")
    public RequestResult delRole(Integer roleId, HttpServletRequest request) {

        logService.addAdminLog(request.getHeader("adminID"), "删除角色-" + roleId);

        RequestResult result = new RequestResult();
        int i = systemService.delRole(roleId);
        if (i == 1) {
            result.setCode(new StatusCode(200, "删除成功"));
            List<Role> list = systemService.getRoleList();
            result.appendData("roleList", list);
        }
        return result;
    }

    @PutMapping("role/editRole")
    @ApiOperation("编辑角色")
    public RequestResult editRole(Integer roleId, String roleName,HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"编辑角色ID-"+roleId);

        RequestResult result = new RequestResult();
        int i = systemService.editRole(roleId, roleName);
        if(i==1){
            result.setCode(new StatusCode(200, "操作成功"));
            List<Role> list = systemService.getRoleList();
            result.appendData("roleList", list);
        }
        return result;
    }
}
